#include "mainwindow.h"

#include <qvariant.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qapplication.h>
#include <qwhatsthis.h>
#include <qaction.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qtoolbar.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qmessagebox.h> 
#include <qstatusbar.h>
#include <qcolor.h>
#include <qpainter.h>
#include <game_controller.h>

//##################################################################

void GameWindow::setStatusMessage (QString msg) {
	statusBar()->message(msg, -1);
}

GameWindow::GameWindow(GameController * cnt, QWidget* parent, const char* name, WFlags fl )
    : QMainWindow( parent, name, fl ), gameController (cnt)
{	
	setIcon (QPixmap::fromMimeSource ("images/appicon.png"));

    QStatusBar * myStatusBar = statusBar();



    if ( !name )
	setName( "TheDots" );

    // actions

	gameNewAction = new QAction (this, "gameNewAction");
	gameNewAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameNew.png" ) ) );

	//gameEndForCurrentAction = new QAction (this, "gameEndForCurrentAction");
	//gameEndForCurrentAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameEndForCurrent.png" ) ) );

	gameEndForAllAction = new QAction (this, "gameEndForAllAction");
	gameEndForAllAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameEndForAll.png" ) ) );

	gameResetAction = new QAction (this, "gameResetAction");
	gameResetAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameReset.png" ) ) );

	gameSettingsAction = new QAction (this, "gameSettingsAction");
	gameSettingsAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameSettings.png" ) ) );

	gameExitAction = new QAction (this, "gameExitAction");
	gameExitAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameExit.png" ) ) );

	infoRatingAction = new QAction (this, "infoRatingAction");
	infoRatingAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/infoRating.png" ) ) );

	helpRulesAction = new QAction (this, "helpRulesAction");
	helpRulesAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/helpRules.png" ) ) );

	helpContentsAction = new QAction (this, "helpContentsAction");
	helpContentsAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/helpContents.png" ) ) );

	helpAboutAction = new QAction (this, "helpAboutAction");
	helpAboutAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/helpAbout.png" ) ) );
    
	gameLogoutAction = new QAction (this, "gameLogoutAction");
	//gameLogoutAction->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/helpAbout.png" ) ) );

	gameGlobalSettings = new QAction (this, "gameGlobalSettings");
	gameGlobalSettings->setIconSet (QIconSet( QPixmap::fromMimeSource( "images/gameGlobalSettings.png" ) ) );

    // toolbars
    toolBar = new QToolBar( QString(""), this, DockTop ); 

	toolBar->setFrameShape (QFrame::Shape::WinPanel);
	toolBar->setFrameShadow (QFrame::Shadow::Raised);
	
	//layout
    //game
	gameLayout = new QWidget (this);
	this->setCentralWidget (gameLayout);

	vLayout = new QVBoxLayout (gameLayout, 5, 5);
	playersList = new PlayersList (gameController, gameLayout);
	field = new GameField (gameController, gameLayout);

	hLayout = new QHBoxLayout (gameLayout, 5, 5);
	hLayout->addWidget (field, 1, Qt::AlignCenter);
	hLayout->addWidget (playersList, 0, Qt::AlignRight);
	vLayout->addLayout (hLayout);
	vLayout->addWidget (myStatusBar);

	statusIndicator = new GameStatusIndicator (gameLayout);
	statusIndicator->setPlayerInfo ("No players!", QColor("red"));
	
	myStatusBar->addWidget (statusIndicator, 0, true);

	//empty
	//emptyLayout = new QWidget (this);
	//this->setCentralWidget (emptyLayout);

	//logoLabel = new QLabel ("<b><h2>Use application menu to start the game.</h2></b>", emptyLayout);
	logoLabel = new QLabel (this);
	logoLabel->setAlignment (Qt::AlignCenter);
	logoLabel->setPixmap (QPixmap::fromMimeSource ("images/mainwindow.png"));
	logoLabel->setGeometry (0, 0, 640, 480);
	emptyLayout = logoLabel;
	

	this->setSizePolicy (QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
	
	//rating
	ratingLayout = new QWidget (this);
	vRatingLayout = new QVBoxLayout (ratingLayout, 5, 5);
	hRatingLayout = new QHBoxLayout (ratingLayout, 5, 5);
	ratingTable = new QTable (10, 4, ratingLayout);
	ratingTable->setRowMovingEnabled ( false ); 
	ratingTable->setDragEnabled (false);
	ratingTable->setReadOnly (true);
	ratingTable->setColumnMovingEnabled (false);
	ratingTable->setRowMovingEnabled (false);
	


	saveRatingButton   = new QPushButton ("Save to file...", ratingLayout);
	loadRatingButton   = new QPushButton ("Load from file...", ratingLayout);
	exportRatingButton = new QPushButton ("Export...", ratingLayout);
	closeRatingButton  = new QPushButton ("Close", ratingLayout);
	hRatingLayout->addWidget (saveRatingButton);
	hRatingLayout->addWidget (loadRatingButton);
	hRatingLayout->addWidget (exportRatingButton);
	hRatingLayout->addWidget (closeRatingButton);
	vRatingLayout->addWidget (ratingTable);
	vRatingLayout->addLayout (hRatingLayout);


	gameNewAction->addTo (toolBar);
	gameEndForAllAction->addTo (toolBar);
	gameSettingsAction->addTo (toolBar);
	gameExitAction->addTo (toolBar);
	infoRatingAction->addTo (toolBar);
	helpContentsAction->addTo (toolBar);

    // menubar
    MenuBar = new QMenuBar( this, "MenuBar" );

	//MenuBar->setFrameShape (QFrame::Shape::WinPanel);
	//MenuBar->setFrameShadow (QFrame::Shadow::Raised);

    gameMenu = new QPopupMenu( this );
	gameNewAction->addTo (gameMenu);
	//gameEndForCurrentAction->addTo (gameMenu);
	gameEndForAllAction->addTo (gameMenu);
	gameResetAction->addTo (gameMenu);
	gameMenu->insertSeparator ();
	gameSettingsAction->addTo (gameMenu);
	gameGlobalSettings->addTo (gameMenu);
	gameMenu->insertSeparator ();
	//gameLogoutAction->addTo (gameMenu);
	gameExitAction->addTo (gameMenu);

    MenuBar->insertItem( QString(""), gameMenu, 1 );

	infoMenu = new QPopupMenu( this );
	infoRatingAction->addTo (infoMenu);

	MenuBar->insertItem (QString(""), infoMenu, 2);

    helpMenu = new QPopupMenu( this );
    helpContentsAction->addTo( helpMenu );
	helpRulesAction->addTo (helpMenu);
    helpMenu->insertSeparator();
    helpAboutAction->addTo( helpMenu );
    MenuBar->insertItem( QString(""), helpMenu, 3 );

    languageChange();
    resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
    clearWState( WState_Polished );


    // signals and slots connections
    connect( gameNewAction, SIGNAL( activated() ), gameController, SLOT( gameNew() ) );
    //connect( gameEndForCurrentAction, SIGNAL( activated() ), gameController, SLOT( gameEndForCurrent() ) );
    connect( gameEndForAllAction, SIGNAL( activated() ), gameController, SLOT( gameEndForAll() ) );
    connect( gameResetAction, SIGNAL( activated() ), gameController, SLOT( gameReset() ) );
    connect( gameSettingsAction, SIGNAL( activated() ), gameController, SLOT( gameSettings() ) );
    connect( gameExitAction, SIGNAL( activated() ), gameController, SLOT( gameExit() ) );
	connect( gameLogoutAction, SIGNAL( activated() ), gameController, SLOT( gameLogout() ) );
	connect( gameGlobalSettings, SIGNAL( activated() ), gameController, SLOT( gameGlobalSettings() ) );
    connect( infoRatingAction, SIGNAL( activated() ), gameController, SLOT( infoRating() ) );
    connect( helpRulesAction, SIGNAL( activated() ), gameController, SLOT( helpRules() ) );
    connect( helpContentsAction, SIGNAL( activated() ), gameController, SLOT( helpContents() ) );
    connect( helpAboutAction, SIGNAL( activated() ), gameController, SLOT( helpAbout() ) );
}

/*
 *  Destroys the object and frees any allocated resources
 */
GameWindow::~GameWindow()
{
    // no need to delete child widgets, Qt does it all for us
}


void GameWindow::setController (GameController * c) {
	this->gameController = c;
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void GameWindow::languageChange()
{
    setCaption( tr( "TheDots" ) );
	gameNewAction->setText ( tr("New"));
	gameNewAction->setMenuText ( tr("&New"));

	gameEndForAllAction->setText (tr ("End for all"));
	gameEndForAllAction->setMenuText (tr ("End for all"));

	gameResetAction->setText ( tr("Reset"));
	gameResetAction->setMenuText ( tr("Reset"));

	gameSettingsAction->setText ( tr("Profile settings"));
	gameSettingsAction->setMenuText ( tr("Profile settings"));

	gameExitAction->setText ( tr("Exit"));
	gameExitAction->setMenuText ( tr("Exit"));

	gameGlobalSettings->setText ( tr("Field settings..."));
	gameGlobalSettings->setMenuText ( tr("Field settings..."));

    gameLogoutAction->setText ( tr("Change profile..."));
	gameLogoutAction->setMenuText ( tr("Change profile..."));


	infoRatingAction->setText ( tr("Rating"));
	infoRatingAction->setMenuText ( tr("Rating"));

	helpRulesAction->setText ( tr("Rules"));
	helpRulesAction->setMenuText ( tr("Rules"));

	helpContentsAction->setText ( tr("Contents"));
	helpContentsAction->setMenuText ( tr("Contents"));

	helpAboutAction->setText ( tr("About"));
	helpAboutAction->setMenuText ( tr("About"));

    toolBar->setLabel( tr( "Actions" ) );

    if (MenuBar->findItem(1))
        MenuBar->findItem(1)->setText( tr( "&Game" ) );
    if (MenuBar->findItem(2))
        MenuBar->findItem(2)->setText( tr( "&Info" ) );
	if (MenuBar->findItem(3))
        MenuBar->findItem(3)->setText( tr( "&Help" ) );


	ratingTable->horizontalHeader()->setLabel (0, "Name");
	ratingTable->horizontalHeader()->setLabel (1, "Won dots");
	ratingTable->horizontalHeader()->setLabel (2, "Lost dots");
	ratingTable->horizontalHeader()->setLabel (3, "Time played(in min)");

}

void GameWindow::closeEvent ( QCloseEvent * e ) {
	this->gameController->gameExit();
}